const pool = require('./db.js')
class userDao {
  // 根据账号与密码，查询是否存在这个用户
  async getUser(account, pwd) {
    try {
      let result = {}
      result = await pool.execute('select * from users where account=? and password=?', [account, pwd])
      return result
    } catch (err) {
      console.log('出错:' + err)
    }
  }
  async getUserById(id) {
    const [rows] = await pool.query('SELECT * FROM users WHERE id = ?', [id]);
    return rows[0];
  }  // 新增用户 输入用户账号，密码，用户名
  async addUser(name, account, pwd) {
    try {
      let result = await pool.execute('insert users(username,password,account) values(?,?,?)', [name, pwd, account])
      return result
    } catch (err) {
      console.log('出错:' + err)
    }
  }
  // 根据用户id获取用户信息
  async findUser(id){
    try{
      let result = await pool.execute('select * from users where id=?',[id])
      return result
    }catch(err){
      console.log('出错:' + err)
    }
  }
}

module.exports = new userDao()